package com.cuit.zuke.data.controller;


import com.cuit.zuke.common.util.Result;
import com.cuit.zuke.data.model.entity.Dict;
import com.cuit.zuke.data.service.DictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/data/dict")
public class DictController {

    @Autowired
    private DictService dictService;

    /**
     * 根据id查询其之下的子节点
     * @param id id
     * @return 子节点
     */
    @GetMapping(value = "findChildData/{id}")
    public Result findChildData(@PathVariable Long id){
        List<Dict> dictList = dictService.findChildData(id);

        return Result.ok().put("data",dictList);
    }

    /**
     * 根据dictCode查询下级节点
     * @param dictCode dictCode
     * @return 下级节点
     */
    @GetMapping(value = "findByDictCode/{dictCode}")
    public Result findByDictCode(@PathVariable String dictCode){
        List<Dict> dictList = dictService.findByDictCode(dictCode);

        return Result.ok().put("data",dictList);
    }

    /**
     * 导出数据字典
     * @param response
     * @return 返回excel表数据
     */
    @GetMapping(value = "exportData")
    public void exportData(HttpServletResponse response){
        dictService.exportDictData(response);
    }

    /**
     * 导入数据字典
     * @param file excel文件
     * @return 1 or 0
     */
    @PostMapping(value = "importData")
    public Result importDict(MultipartFile file){
        dictService.importDictFile(file);
        return Result.ok("success");
    }

    /**
     * 根据dictCode和value查询对应的静态数据名称
     * @param dictCode dictCode
     * @param value value
     * @return 静态数据名称
     */
    @GetMapping(value = "getName/{dictCode}/{value}")
    public Result getName(@PathVariable String dictCode, @PathVariable String value){
        String dictName = dictService.getDictName(dictCode,value);

        return Result.ok().put("data",dictName);
    }

    /**
     * 根据value值查询对应的静态数据名称
     * @param value value
     * @return 静态数据名称
     */
    @GetMapping(value = "getName/{value}")
    public Result getName(@PathVariable String value){
        String dictName = dictService.getDictName("",value);

        return Result.ok().put("data",dictName);
    }

    // 根据id查询name
    @GetMapping(value = "/getNameById/{id}")
    String getNameById(@PathVariable("id") Long id){
        String dictName = dictService.getNameById(id);
        return dictName;
    }


}
